package com.bluefuture.modules.app.service.impl;

import com.bluefuture.modules.app.entity.UserEntity;
import com.bluefuture.modules.app.service.UserService;
import com.bluefuture.modules.sys.dao.SysUserTokenDao;
import com.bluefuture.modules.sys.entity.SysUserTokenEntity;
import com.bluefuture.modules.sys.security.user.SysUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author Clown
 * @Date 2021-08-28 23:00
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private SysUserTokenDao sysUserTokenDao;

    @Override
    public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException {
        UserEntity userEntity = userService.getById(Long.valueOf(user));
        return new SysUserDetails(userEntity.getUserId(), userEntity.getUserId().toString(), userEntity.getPassword(), true, true, true, true, new ArrayList<>());
    }

    public SysUserTokenEntity queryByToken(String token) {
        return sysUserTokenDao.queryByToken(token);
    }

}
